package com.echo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.activeandroid.query.Select;
import com.echo.adapter.HistoryDayRecordAdapter;
import com.echo.model.DayData;
import com.echo.model.Sport;
import com.echo.util.DateUtil;
import com.echo.util.DrawableUtil;
import com.echo.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class HitoryDayRecordActivity extends AppCompatActivity {

    private DayData mDayData;
    private String mDate;

    private TextView mCalories,mStep,mTime,mDistance;

    private List<Sport> mSports=new ArrayList<Sport>();

    private RecyclerView mSportsRecycler;

    private HistoryDayRecordAdapter mAdapter;
    private Toolbar mToolbar;
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hitory_day_record);

        initData();
        initView();
        initActionBar();
    }

    private void initActionBar() {
        mToolbar= (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setNavigationIcon(DrawableUtil.tintDrawable(getResources()
                .getDrawable(R.mipmap.ic_arrow_back_black_36dp), Color.parseColor("#8a041835")));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle= (TextView) findViewById(R.id.toolbar_title);
        mTitle.setText(DateUtil.getDateStr1(mDayData.getDate()));
    }

    private void initData() {
        Intent intent=getIntent();
        Bundle bundle=intent.getBundleExtra("key");
        mDate=bundle.getString("date");
        mDayData=new Select().from(DayData.class).where("date=?",mDate).orderBy("RANDOM()").executeSingle();
        mSports=new Select().from(Sport.class).where("date=?",mDate).orderBy("start_time asc").execute();
    }

    private void initView() {
        mStep= (TextView) findViewById(R.id.day_step);
        mCalories= (TextView) findViewById(R.id.day_calories);
        mTime= (TextView) findViewById(R.id.day_time);
        mDistance= (TextView) findViewById(R.id.day_distance);
        mCalories.setText(String.valueOf(mDayData.getCalories()));
        mStep.setText(String.valueOf(mDayData.getSteps()));
        mTime.setText(String.valueOf(TimeUtil.secondsToStr(mDayData.getTime())));
        mDistance.setText(String.valueOf(mDayData.getDistance()));
        mSportsRecycler= (RecyclerView) findViewById(R.id.history_day_record_recycler);
        mSportsRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mAdapter=new HistoryDayRecordAdapter(this,mSports);
        mSportsRecycler.setAdapter(mAdapter);
    }
}
